package com.hns.can.service.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.service.DriverExamService;
import com.hns.can.service.service.SecurityGradeService;
import com.hns.can.service.vo.DriverCommentParamsVo;
import com.hns.can.service.vo.SecurityGradeVo;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelTemplate.ExportExcelTempEntity;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 违章事故考核controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/securityBehaviour")
public class SecurityGradeController extends BaseController {

    @Autowired
    private SecurityGradeService securityGradeService;
    @Autowired
    private DriverExamService driverExamService;

    final private String[] headerNames = new String[]{"排名","事故得分","违章得分","总得分","驾驶员","运营线路","所属机构","运行时长","运行里程(KM)","<300","300-1000",">1000","违章次数","夜间行车","起步急加","超速"};
    final private String[] headerFields = new String[]{"allRank","accdtGrade","illegalGrade","totalGrade","drvName","lineName","organName","totalDura","totalMile","summary1","summary2","summary3","summary4","summary5","summary6","summary7"};

    private List<SecurityGradeVo> getSecurityGradeInfo(CommonParamsVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        driverExamService.initParams(vo, user, "T_COMM_DRV_SCT_BHV_INFO");
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(), vo);
        List<SecurityGradeVo> list = securityGradeService.getSecurityGradeInfo(vo);
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        return list;
    }

    /**
     * 导出违章事故考核数据Excel文档
     *
     * @param params
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportSecurityBhvExcel")
    public void exportSecurityBhvExcel(CommonParamsVo params,HttpServletResponse response) throws Exception {
        List<SecurityGradeVo> list = getSecurityGradeInfo(params);
        ExportExcelTempEntity entity=new ExportExcelTempEntity(list,"securityGradeTemplate.xls",URLEncoder.encode("违章事故考核", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelByTemplate(entity,response);
    }

    /**
     * 导出违章事故考核数据Pdf文档
     *
     * @param params
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportSecurityBhvPdf")
    public void exportSecurityBhvPdf(CommonParamsVo params,HttpServletResponse response) throws Exception {
        List<SecurityGradeVo> list = getSecurityGradeInfo(params);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", URLEncoder.encode("违章事故考核", "UTF-8") + DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "违章事故数据");
    }

    /**
     * 分页查询违章事故考核数据
     *
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchSecurityBehaviour")
    @ResponseBody
    public Object searchSecurityBehaviour(CommonParamsVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        driverExamService.initParams(vo, user, "T_COMM_DRV_SCT_BHV_INFO");
        driverExamService.getReportDate(vo.getBeginTime(), vo.getReportType(), vo);
        Pagination page = securityGradeService.searchSecurityBehaviour(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 获取驾驶员违章事故考核评语
     *
     * @param vo
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getSecurityComments")
    @ResponseBody
    public Object getEnerComments(DriverCommentParamsVo vo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        //数组：{评语，评价级别}
        List<String> list = driverExamService.getComments(vo, user, new String[]{"getCommentContent4Safe","getCommentGrade4Safe"});
        return this.getReturnResult(list);
    }

}
